// ActionScript file
import jpa.domain.Department;

import mx.controls.Alert;
import mx.rpc.events.FaultEvent;
import mx.rpc.events.ResultEvent;
import mx.rpc.remoting.mxml.RemoteObject;
		
		[Bindable]
		public var department:Department;
		private var ro:RemoteObject;

		private function save():void
		{
			department.name = departmentName.text;

			if (department.id == 0)
			{
			ro=Global.getRemoteObject("departmentController",true,create_resultHandler);
			ro.create(department);
			}
			else
			{
			ro=Global.getRemoteObject("departmentController",true,update_resultHandler);
			ro.update(department);
			}
				
		}

		private function deleteItem():void
		{
			ro.remove(department);		
		}

		private function create_resultHandler(event:ResultEvent):void
		{
			//carInfo.id = event.result.id;
			dispatchEvent(new DepartmentEvent(DepartmentEvent.CREATED, department));			
		}

		private function update_resultHandler(event:ResultEvent):void
		{
			dispatchEvent(new DepartmentEvent(DepartmentEvent.UPDATED, department));			
		}

		private function remove_resultHandler(event:ResultEvent):void
		{
			dispatchEvent(new DepartmentEvent(DepartmentEvent.DELETED, department));			
		}
		
		private function faultHandler(event:FaultEvent):void
		{
			Alert.show(event.fault.faultDetail);
		}